﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Imports DataObject
Imports System.Data.SqlClient

Public Class SqlServerViTriCongViec

    Private oqLBase As New hDBase
    Private oDataSet As New DataSet
    Private oDatatable As New DataTable

    Public Function GetAllViTriCongViec(ByVal sConnString As String) As DataTable
        Try
            oqLBase.Connection(sConnString)
            oDataSet = oqLBase.GetData("Get_All_ViTriCongViecs", "ViTriCongViec")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return oDataSet.Tables("ViTriCongViec")
    End Function

    Public Function InsertViTriCongViec(ByVal sConnString As String, ByVal oViTriCongViec As ViTriCongViec) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoViTriCV", SqlDbType.Int), _
                                           New SqlParameter("@SoNhomViTri", SqlDbType.Int), _
                                           New SqlParameter("@SoChucDanh", SqlDbType.Int), _
                                           New SqlParameter("@MaViTriCV", SqlDbType.VarChar, 20), _
                                           New SqlParameter("@TenViTriCV", SqlDbType.NVarChar, 50), _
                                           New SqlParameter("@DienGiai", SqlDbType.NVarChar, 255), _
                                           New SqlParameter("@HoatDong", SqlDbType.Bit)}
        SqlParam(0).Direction = ParameterDirection.Output
        SqlParam(1).Value = oViTriCongViec.SoNhomViTri
        SqlParam(2).Value = oViTriCongViec.SoChucDanh
        SqlParam(3).Value = oViTriCongViec.MaViTriCV
        SqlParam(4).Value = oViTriCongViec.TenViTriCV
        SqlParam(5).Value = oViTriCongViec.DienGiai
        SqlParam(6).Value = oViTriCongViec.HoatDong
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Insert_ViTriCongViec", "ViTriCongViec", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function UpdateViTriCongViec(ByVal sConnString As String, ByVal oViTriCongViec As ViTriCongViec) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoViTriCV", SqlDbType.Int), _
                                                   New SqlParameter("@SoNhomViTri", SqlDbType.Int), _
                                                   New SqlParameter("@SoChucDanh", SqlDbType.Int), _
                                                   New SqlParameter("@MaViTriCV", SqlDbType.VarChar, 20), _
                                                   New SqlParameter("@TenViTriCV", SqlDbType.NVarChar, 50), _
                                                   New SqlParameter("@DienGiai", SqlDbType.NVarChar, 255), _
                                                   New SqlParameter("@HoatDong", SqlDbType.Bit)}
        SqlParam(0).Value = oViTriCongViec.SoViTriCV
        SqlParam(1).Value = oViTriCongViec.SoNhomViTri
        SqlParam(2).Value = oViTriCongViec.SoChucDanh
        SqlParam(3).Value = oViTriCongViec.MaViTriCV
        SqlParam(4).Value = oViTriCongViec.TenViTriCV
        SqlParam(5).Value = oViTriCongViec.DienGiai
        SqlParam(6).Value = oViTriCongViec.HoatDong
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Update_ViTriCongViec", "ViTriCongViec", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function DeleteTinhTrangHopDong(ByVal sConnString As String, ByVal SoViTriCongViec As Integer) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoViTriCongViec", SqlDbType.Int)}
        SqlParam(0).Value = SoViTriCongViec
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Delete_ViTriCongViec", "ViTriCongViec", SqlParam)
            Return True
        Catch ex As SqlException
            Select Case ex.ErrorCode
                Case -2146232060
                    MessageBox.Show("Không thể xóa dữ liệu này. Loại vị trí công việc này đang được sử dụng trong phần nghiệp vụ khác", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return False
            End Select
        End Try
    End Function

End Class
